﻿using Newtonsoft.Json;
using SocketIOClient;
using SocketioTest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;

namespace socketio.socketio
{
    public class Socketa
    {

        public static SocketIO client;
        Data dto = new Data();

        public Socketa()
        {
           
            if (Global.socketIoServerUrl.Length==0)
            {
                MessageBox.Show("请配置socket服务地址");
                return;
            }
            
            StartSocketAsync();
        }

        public async Task StartSocketAsync()
        {
            client = new SocketIO(Global.socketIoServerUrl, new SocketIOOptions
            {
                Query = new List<KeyValuePair<string, string>>
                {
                    //new KeyValuePair<string, string>("token", "abc123"),
                    //new KeyValuePair<string, string>("key", "value")
                },
                EIO=3,
                Reconnection = true, 
            });

            client.On("error", response =>
            {
                // You can print the returned data first to decide what to do next.
                // output: ["hi client"]
                Console.WriteLine("error");
                //Console.WriteLine(response);

                string text = response.GetValue<string>();

                // The socket.io server code looks like this:
                // socket.emit('hi', 'hi client');
            });

            client.On("test", response =>
            {
                // You can print the returned data first to decide what to do next.
                // output: ["ok",{"id":1,"name":"tom"}]
                Console.WriteLine(response);

                // Get the first data in the response
                string text = response.GetValue<string>();
                // Get the second data in the response
                var dto = response.GetValue<Data>(1);

                // The socket.io server code looks like this:
                // socket.emit('hi', 'ok', { id: 1, name: 'tom'});
            });

            client.On("session", response =>
            {
                // You can print the returned data first to decide what to do next.
                // output: ["ok",{"id":1,"name":"tom"}]
                Console.WriteLine(response);
                Console.WriteLine("session+++");

                // Get the first data in the response
                //string text = response.GetValue<string>();
                // Get the second data in the response
                //var dto = response.GetValue<Data>(1);

                // The socket.io server code looks like this:
                // socket.emit('hi', 'ok', { id: 1, name: 'tom'});
            });

            client.OnConnected += async (sender, e) =>  //初始化链接 可直接发送指令
            {
                //Console.WriteLine(sender);
                // Emit a string
                //await client.EmitAsync("hi", "socket.io");
                //await client.EmitAsync("hi", "socket.io");
                // Emit a string and an object

                //await client.EmitAsync("hi", "onAny");
                //await client.EmitAsync("session", "source", dto);
                //await client.EmitAsync("login", dto);
                // Emit a string

                //dto.userId = "ec865a6a1ba657e2";
                //dto.userName = "bob";
                //dto.userImg = "https://downs.7wtv.com/20201201/5fc5fa881e36c.GIF";
                //string output = JsonConvert.SerializeObject(dto);
                
                dto.userId = Global.uid;
                dto.name = Global.name;
                dto.unit_type = "win";
                dto.avatar = Global.avatar;
                string output = JsonConvert.SerializeObject(dto);
                await client.EmitAsync("login", output);
            };
            await client.ConnectAsync();
        }


        public static async Task SendMsg(object sendText)
        {
            await client.EmitAsync("privateChat", sendText);
        }

        /**
         * 发送群聊
         */
        public async Task SendMsgGroup(String sendText)
        {
            Console.WriteLine(sendText);
            string output = JsonConvert.SerializeObject(new { type = "my", msg = sendText, sender = Global.uid, senderimg = Global.avatar, receiver="默认群聊"});
            Console.WriteLine(output);
            await client.EmitAsync("groupChat", output);
        }

        public async Task StopSocketAsync()
        {

        }
    }
}
